﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Navigation;
using RegOnline.RegOnlineService;
using System.IO;
using System.Text;
using RegOnline.Views;

namespace RegOnline
{
    public partial class PrepodavatelPage : Page
    {


        public PrepodavatelPage()
        {
            InitializeComponent();
        }

        MainViewModel ac;
        // Executes when the user navigates to this page.
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            regDateDatePicker.SelectedDate = DateTime.Now.Subtract(new TimeSpan(30,0,0,0));
            regDateDatePicker1.SelectedDate = DateTime.Now;
            ac = ((((Parent as Frame).Parent as Border).Parent as Grid).Parent as MainPage).Controller;
            ac.GodsLoaded += new EventHandler(ac_GodsLoaded);
            ac.TypeSemLoaded += new EventHandler(ac_TypeSemLoaded);
            ac.PrepodGrupsLoaded += new EventHandler(ac_PrepodGrupsLoaded);
            ac.ReqWorkIserted += new EventHandler(ac_ReqWorkIserted);
            ac.ReqWorksLoaded += new EventHandler(ac_ReqWorksLoaded);
            ac.DisciplineLoaded += new EventHandler(ac_DisciplineLoaded);
            ac.VidWorkLoaded += new EventHandler(ac_VidWorkLoaded);
            ac.ReqWorksLoaded += new EventHandler(ac_ReqWorksLoaded);
            ac.WorksLoaded += new EventHandler(ac_WorksLoaded);

            ac.LoadGodUP(); 
            ac.LoadTypeSem(); 

        }

        void ac_WorksLoaded(object sender, EventArgs e)
        {
            worksDTODataGrid1.ItemsSource = ac.Works;

            if (ac.Works.Count == 0)
            {
                rectangle2.Visibility = System.Windows.Visibility.Visible;
                label2.Visibility = System.Windows.Visibility.Visible;
            }
            else
            {
                rectangle2.Visibility = System.Windows.Visibility.Collapsed;
                label2.Visibility = System.Windows.Visibility.Collapsed;
            }
        }

        void ac_VidWorkLoaded(object sender, EventArgs e)
        {
            vidWorkNameComboBox.ItemsSource = ac.VidWorks;
            vidWorkNameComboBox.SelectedItem = ac.CurrentVidWork;
            
        }

        void ac_DisciplineLoaded(object sender, EventArgs e)
        {
            disNameComboBox.ItemsSource = ac.Disciplines;
            disNameComboBox.SelectedItem = ac.CurrentDiscipline;
        }

        void ac_ReqWorksLoaded(object sender, EventArgs e)
        {
            reqsWorkDTODataGrid1.ItemsSource = ac.ReqWorks;
            
                if (ac.ReqWorks.Count == 0)
                {
                    rectangle3.Visibility = System.Windows.Visibility.Visible;
                    label3.Visibility = System.Windows.Visibility.Visible;
                }
                else
                {
                    rectangle3.Visibility = System.Windows.Visibility.Collapsed;
                    label3.Visibility = System.Windows.Visibility.Collapsed;
                }
           
        }

        void ac_ReqWorkIserted(object sender, EventArgs e)
        {
            reqsWorkDTODataGrid1.ItemsSource = ac.ReqWorks;
        }

        void ac_PrepodGrupsLoaded(object sender, EventArgs e)
        {
            foreach(var g in ac.Groups)
            {
                foreach (var uchPlan in g.Up)
                {
                    uchPlan.StatusImageUrl = ImageController.GetPict(uchPlan);
                }
            }
            grupNameComboBox1.ItemsSource = ac.Groups;
            grupNameComboBox2.ItemsSource = ac.Groups;
            
        }

        void ac_TypeSemLoaded(object sender, EventArgs e)
        {
            nameSemTypeComboBox.ItemsSource = ac.TypeSems;
            nameSemTypeComboBox1.ItemsSource = ac.TypeSems;

            nameSemTypeComboBox.SelectedItem = ac.TypeSems.First();
            nameSemTypeComboBox1.SelectedItem = ac.TypeSems.First();

        }

        void ac_GodsLoaded(object sender, EventArgs e)
        {
            nameGodComboBox.ItemsSource = ac.Gods;
            nameGodComboBox1.ItemsSource = ac.Gods;

            nameGodComboBox.SelectedItem = ac.Gods.First();
            nameGodComboBox1.SelectedItem = ac.Gods.First();
           
        }

        private void nameSemTypeComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

            var typesem = nameSemTypeComboBox.SelectedItem as TypeSem;
            ac.CurrentTypeSem = typesem;
            nameSemTypeComboBox1.SelectedItem = typesem; 
            if (!(ac.CurrentGod == null || typesem == null)) ac.LoadGrupPrepod(typesem.IdTypeSem);

        }

        private void grupNameComboBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            ac.CurrentGroup = grupNameComboBox1.SelectedItem as RegOnlineService.Group;
            grupNameComboBox2.SelectedItem = ac.CurrentGroup;
            
            try
            {
                foreach (var v in ac.CurrentGroup.Up)
                {
                    v.StatusImageUrl = ImageController.GetPict(v,ImageController.ImageControllerUser.prepod);
                }

                upDataGrid1.ItemsSource = ac.CurrentGroup.Up;
            }
            catch
            {
            }
        }

        private void upDataGrid1_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            try
            {
                var G = nameGodComboBox.SelectedItem as godUP;
                var v = nameSemTypeComboBox.SelectedItem as TypeSem;
                G.IdGod = ac.CurrentGod.IdGod;
                v = ac.CurrentTypeSem;
                if (upDataGrid1.SelectedIndex != -1)
                {
                    ac.CurrentUchPlan = (upDataGrid1.SelectedItem as UchPlan);
                }

                if (!(ac.CurrentUchPlan.Vid.IdVid == null || ac.CurrentUchPlan.Dis.IdDis == null))
                {
                    ac.LoadReqWorkForPrepod(G.IdGod, v.IdTypeSem, ac.CurrentUchPlan.Dis.IdDis, ac.CurrentUchPlan.Vid.IdVid);
                  
                }

            }
            catch
            {
            }
               

        }

        private void button5_Click(object sender, RoutedEventArgs e)
        {
            if (upDataGrid1.SelectedIndex != -1)
            {
                DobTrebChildWindow DT = new DobTrebChildWindow();
                DT.ac = ac;
                DT.Show();
                DT.Closing += new EventHandler<System.ComponentModel.CancelEventArgs>(DT_Closing);
                DT.Closed += new EventHandler(DobTrebChildWindow_Closed);
            }
            else
            {
                MessageWindow MW = new MessageWindow("Сначало выбирите работу из учебного плана!");
                MW.Show(); 
            }

        }

        void DT_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            DobTrebChildWindow dw = (DobTrebChildWindow)sender;

            if (dw.DialogResult == true)
                try
                {
             
                    ac.CurrentGod = (nameGodComboBox.SelectedItem as godUP); 
                    ac.InsertReqWork(dw.selectedRefreq.IdRefReq, dw.expdocTextBox.Text);
                    ac.LoadReqWorkForPrepod(ac.CurrentGod.IdGod, ac.CurrentTypeSem.IdTypeSem, ac.CurrentUchPlan.Dis.IdDis, ac.CurrentUchPlan.Vid.IdVid);

                }

                catch (Exception ex)
                {
                    ChildWindow errorWin = new ErrorWindow(ex);
                    errorWin.Show();
                }
        }

        void DobTrebChildWindow_Closed(object sender, EventArgs e)
        {
            
        }


        private void btnDelete_Click(object sender, RoutedEventArgs e)
        {
            var r = reqsWorkDTODataGrid1.SelectedItem as ReqsWork;
            if (r.IdListReq != null)
            {
                ac.DeleteListReq(r);
                ac.CurrentGod = (nameGodComboBox.SelectedItem as godUP);
                ac.LoadReqWorkForPrepod(ac.CurrentGod.IdGod, ac.CurrentTypeSem.IdTypeSem, ac.CurrentUchPlan.Dis.IdDis, ac.CurrentUchPlan.Vid.IdVid);
            }
        }



        private void nameSemTypeComboBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            var typesem = nameSemTypeComboBox1.SelectedItem as TypeSem;
            ac.CurrentTypeSem = typesem;
            nameSemTypeComboBox.SelectedItem = typesem;
            if (!(ac.CurrentGod == null || typesem == null)) ac.LoadGrupPrepod(typesem.IdTypeSem);
        }

        private void grupNameComboBox2_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            ac.CurrentGroup = grupNameComboBox2.SelectedItem as RegOnlineService.Group;
            grupNameComboBox1.SelectedItem = ac.CurrentGroup;
            if (!(ac.CurrentGod == null || ac.CurrentTypeSem == null || ac.CurrentGroup == null))
            ac.LoadDiscipline(ac.CurrentGod.IdGod, ac.CurrentTypeSem.IdTypeSem, ac.CurrentGroup.IdGrup, ac.User.IdUser);
        }

        private void regDateDatePicker1_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
        {
        }

        private void disNameComboBox_MouseRightButtonUp(object sender, MouseButtonEventArgs e)
        {
        }

        private void disNameComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            ac.CurrentDiscipline = disNameComboBox.SelectedItem as Discipline;
            if (!(ac.CurrentGod == null || ac.CurrentTypeSem == null || ac.CurrentGroup == null || ac.CurrentDiscipline == null))
            ac.LoadVidWork(ac.CurrentGod.IdGod, ac.CurrentTypeSem.IdTypeSem, ac.CurrentGroup.IdGrup, ac.CurrentDiscipline.IdDis, ac.User.IdUser);
        }

        private void nameGodComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            ac.CurrentGod = nameGodComboBox.SelectedItem as RegOnlineService.godUP;
            nameGodComboBox.SelectedItem = ac.CurrentGod;
            var typesem = nameSemTypeComboBox.SelectedItem as TypeSem;
            if (!(ac.CurrentGod==null || typesem == null)) ac.LoadGrupPrepod(typesem.IdTypeSem);
        }

        

        private void nameGodComboBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            ac.CurrentGod = nameGodComboBox1.SelectedItem as RegOnlineService.godUP;
            nameGodComboBox.SelectedItem = ac.CurrentGod;
            var typesem = nameSemTypeComboBox.SelectedItem as TypeSem;
            if (!(ac.CurrentGod == null || typesem == null)) ac.LoadGrupPrepod(typesem.IdTypeSem);
        }

        private void vidWorkNameComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            ac.CurrentVidWork = vidWorkNameComboBox.SelectedItem as VidWork;

        }

        private void button9_Click(object sender, RoutedEventArgs e)
        {
            SaveFileDialog SFD = new SaveFileDialog();
            SFD.Filter = " html - файл |*.html";
            SFD.FilterIndex = 1;

            if ((bool)SFD.ShowDialog())
            {
                using (StreamWriter SW = new StreamWriter(SFD.OpenFile(), Encoding.UTF8))
                {




                    //записать шапку
                    string header = @"<html><Title>Список зарегистрированных работ</Title><Body>";
                    string footer = @"</body></html>";
                    SW.Write(header);

                    //дисциплина, вид работы, препод, специальность, интервал
                    string hep = @"<font size = 5><center><b>Зарегистрированные работы</b></center></font> <br>" +
                        "<font size = 4><b>Дисциплина: </b></font>" + ac.CurrentDiscipline.DisName +
                        "<font size = 4><b><br>Вид работы: </b></font> " + ac.CurrentVidWork.VidWorkName +
                                 "<font size = 4><b><br>Преподаватель:</b></font> " + ac.User.Fio +
                                 "<font size = 4><b><br>Работы зарегестрированные с</b></font> " + regDateDatePicker.DisplayDate.ToShortDateString() +
                                 "<font size = 4><b> по </b></font>" + regDateDatePicker1.DisplayDate.ToShortDateString() +
                                 "<br>";
                    string otd = "<td>";
                    string ctd = "</td>";

                    SW.Write(hep);

                    //записать предисловие


                    //foreach
                    SW.Write(@"<Table border = 1 cellpadding = 4 cellspacing=0  >");

                    SW.Write(@"<tr><b>");

                    SW.Write(otd + "<b>Номер зачётки</b>" + ctd);
                    SW.Write(otd + "<b>Ф.И.О.</b>" + ctd);
                    SW.Write(otd + "<b>Тема работы</b>" + ctd);
                    SW.Write(otd + "<b>Вариант</b>" + ctd);
                    SW.Write(otd + "<b>Дата регистрации</b>" + ctd);
                    SW.Write(otd + "<b>Регистрационный номер</b>" + ctd);

                    SW.Write(@"</tr>");

                    foreach (var work in ac.Works)
                    {
                        SW.Write(@"<tr>");

                        SW.Write(otd + work.St.zach.nZach+ctd);
                        SW.Write(otd + work.St.person.LastName + " " + work.St.person.FirstName[0] + "." +work.St.person.Patronymic[0] + "." + ctd);
                        SW.Write(otd + work.theme + ctd);
                        SW.Write(otd + (work.options.HasValue ? work.options.Value.ToString():"Не указан") + ctd);
                        SW.Write(otd + work.RegDate.Value.ToShortDateString() + ctd);
                        SW.Write(otd + work.RegNum.Value + ctd);

                        SW.Write(@"</tr>");

                        
                    }

                    SW.Write(@"</Table>");

                    SW.Write(footer);
                    //
                }

            }

        }

        private void worksDTODataGrid1_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

        }

        private void button10_Click_1(object sender, RoutedEventArgs e)
        {
            if (!(ac.CurrentGod == null || ac.CurrentTypeSem == null|| ac.CurrentGroup==null||ac.CurrentDiscipline==null||ac.CurrentVidWork==null))
            ac.LoadOtchet(ac.CurrentGod.IdGod, ac.CurrentTypeSem.IdTypeSem, ac.CurrentGroup.IdGrup, ac.User.IdUser,
            ac.CurrentDiscipline.IdDis, ac.CurrentVidWork.IdVid, regDateDatePicker.SelectedDate.Value, regDateDatePicker1.SelectedDate.Value);

            worksDTODataGrid1.ItemsSource = ac.Works;
            if (ac.Works.Count == 0)
            {
                rectangle2.Visibility = System.Windows.Visibility.Visible;
                label2.Visibility = System.Windows.Visibility.Visible;
            }
            else 
            {
                rectangle2.Visibility = System.Windows.Visibility.Collapsed;
                label2.Visibility = System.Windows.Visibility.Collapsed;
            }

        }

        private void reqsWorkDTODataGrid1_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

        }

    }

    
}
